package com.sgb.kjwl.view.widget.pick;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.BiddingEntity;
import com.sgb.kjwl.view.ui.adapter.pick.PickMapBiddingAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

/**
 * 地图交互信息展示-竞价
 */
public class PickMapBiddingPopWindow extends BasePopWindow {

    private RecyclerView recyclerView;
    private PickMapBiddingAdapter PickMapBiddingAdapter;
    private List<BiddingEntity> BiddingEntityList;
    private CallBack callBack;
    private boolean isAddFinish = false;//数据已加载完成
    private int mPosition;

    public PickMapBiddingPopWindow(Context context, int dir, CallBack callBack) {
        super(context, dir);
        this.callBack = callBack;
    }

    @Override
    protected int popLayout() {
        return R.layout.pick_map_popwindow;
    }

    @Override
    protected void initView(PopViewHolder holder) {

        recyclerView = holder.getPop().findViewById(R.id.rlv_list);

        PickMapBiddingAdapter = new PickMapBiddingAdapter(mContext, null, new PickMapBiddingAdapter.CallBack() {
            @Override
            public void onDismiss() {
                callBack.onDismiss();
                dismiss();
            }

            @Override
            public void onPosition(int position) {
                mPosition = position;
            }

            @Override
            public void onReport(BiddingEntity biddingEntity) {
                callBack.onReport(biddingEntity);
            }

            @Override
            public void onContactService(BiddingEntity biddingEntity) {
                callBack.onContactService(biddingEntity);
            }

            @Override
            public void onInquireByLetter(BiddingEntity biddingEntity) {
                callBack.onInquireByLetter(biddingEntity);
            }

            @Override
            public void onLineUserFocus(BiddingEntity biddingEntity, int position) {
                callBack.onLineUserFocus(biddingEntity,position);
            }

            @Override
            public void onCommit(String id) {
                callBack.onItemClickAdapter(id);
            }
        });
        PickMapBiddingAdapter.setItemSelect(true);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(PickMapBiddingAdapter);

        PagerSnapHelper snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(recyclerView);

        PickMapBiddingAdapter.setOnItemClick(new AdapterOnItemClick<BiddingEntity>() {
            @Override
            public void onItemClick(BiddingEntity biddingEntity, int position) {
                callBack.onItemClickAdapter(biddingEntity.getId());
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {

                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                    //获取完全显示的ItemPosition
                    int lastVisibleItem = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                    //总条数
                    int totalItemCount = PickMapBiddingAdapter.getItemCount();

                    //第九个可见，加载下一页，依此类推     isAddFinish确定加载下一页仅调一次(滑动到9，10，lastVisibleItem均为8)
                    if (lastVisibleItem + 1 == totalItemCount) {
                        if (callBack != null && !isAddFinish) {
                            isAddFinish = true;
                            callBack.onMoreData(true);
                        }
                    }
                    if (lastVisibleItem >= 0) {
                        callBack.onShowPosition(lastVisibleItem);
                    }
                }
            }
        });

    }

    public void setData(List<BiddingEntity> BiddingEntityList, int position) {
        this.BiddingEntityList = BiddingEntityList;
        PickMapBiddingAdapter.setDatas(BiddingEntityList);
        recyclerView.smoothScrollToPosition(position);
        isAddFinish = false;
    }

    public void updateFocus(int mPosition) {
        PickMapBiddingAdapter.updateFocusForPosition(mPosition);
    }

    public interface CallBack {
        void onMoreData(boolean more);

        void onItemClickAdapter(String id);

        void onDismiss();

        void onShowPosition(int position);

        void onReport(BiddingEntity biddingEntity);

        void onContactService(BiddingEntity biddingEntity);

        void onInquireByLetter(BiddingEntity biddingEntity);

        void onLineUserFocus(BiddingEntity biddingEntity, int position);
    }

}
